namespace Plugins.Serializer
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Security;
    using Plugins.Interfaces;
    using Plugins.Library;

    [Serializable]
    public abstract class PluginStringSerializer<T> : PluginStreamSerializer<T>, IPluginStringSerializer<T>
    {
        protected PluginStringSerializer()
        {
        }

        protected abstract byte[] Deserialize(string data);
        protected virtual T Deserialize(string data, IPluginTypeFormatter formatter)
        {
            lock (((PluginStringSerializer<T>) this))
            {
                try
                {
                    byte[] array = this.Deserialize(data);
                    if (array != null)
                    {
                        using (MemoryStream stream = new MemoryStream(array))
                        {
                            if ((formatter != null) && Deserialize(stream, formatter))
                            {
                                return RuntimeObject;
                            }
                        }
                    }
                }
                catch (FileLoadException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IOException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (AccessViolationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ApplicationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FormatException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (InvalidOperationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (Win32Exception ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return default(T);
            }
        }

        protected abstract string Serialize(byte[] data);
        protected virtual string Serialize(T obj, IPluginTypeFormatter formatter)
        {
            lock (((PluginStringSerializer<T>) this))
            {
                try
                {
                    if (!object.Equals(obj, default(T)))
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            if ((formatter != null) && Serialize(obj, stream, formatter))
                            {
                                return this.Serialize(stream.ToArray());
                            }
                        }
                    }
                }
                catch (FileLoadException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IOException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (AccessViolationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ApplicationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FormatException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (InvalidOperationException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (Win32Exception ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return null;
            }
        }

        T IPluginStringSerializer<T>.Deserialize(string data)
        {
            return this.Deserialize(data, XmlTypeFormatter);
        }

        T IPluginStringSerializer<T>.Deserialize(string data, IPluginRuntimeFormatter formatter)
        {
            try
            {
                if (formatter != null)
                {
                    return this.Deserialize(data, formatter);
                }
            }
            catch (ApplicationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            return this.Deserialize(data, XmlTypeFormatter);
        }

        string IPluginStringSerializer<T>.Serialize(T obj)
        {
            return this.Serialize(obj, XmlTypeFormatter);
        }

        string IPluginStringSerializer<T>.Serialize(T obj, IPluginRuntimeFormatter formatter)
        {
            try
            {
                if (formatter != null)
                {
                    return this.Serialize(obj, formatter);
                }
            }
            catch (ApplicationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            return this.Serialize(obj, XmlTypeFormatter);
        }
    }
}

